package yanyan.com.tochar;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SeekBar;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import yanyan.com.tochar.adpter.HomeListAdapter;
import yanyan.com.tochar.adpter.MyFragMentAdapter;
import yanyan.com.tochar.beans.HomeListBean;
import yanyan.com.tochar.fragment.Funs;
import yanyan.com.tochar.fragment.Home;
import yanyan.com.tochar.fragment.Other;
import yanyan.com.tochar.utils.CoreUtil;

import yanyan.com.tochar.utils.PermissionsUtil;

public class MainActivity extends FragmentActivity {

    private ViewPager page;

    private BottomNavigationView navigation;



    private List<Fragment> fragments=new ArrayList<>();




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //取消标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);


        navigation = (BottomNavigationView) findViewById(R.id.navigation);
        page= (ViewPager) findViewById(R.id.viewPage);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        fragments.add(new Home());
        fragments.add(new Funs());
        fragments.add(new Other());
        MyFragMentAdapter myFragMentAdapter=new MyFragMentAdapter(getSupportFragmentManager(),fragments);
        page.setAdapter(myFragMentAdapter);

        // 底部导航栏点击事件
        page.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                switch (i){
                    case 0:
                        navigation.setSelectedItemId(R.id.navigation_home);
                        break;
                    case 1:
                        navigation.setSelectedItemId(R.id.navigation_dashboard);
                        break;
                    case 2:
                        navigation.setSelectedItemId(R.id.navigation_notifications);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //检测更新
        CoreUtil.detectionUpdate(this);
    }

    // 底部导航栏监听事件
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    page.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
                    page.setCurrentItem(1);
                    return true;
                case R.id.navigation_notifications:
                    page.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };


    /**
     * 再按一次退出
     */
    private long elong=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode== KeyEvent.KEYCODE_BACK) {
            if(System.currentTimeMillis()-elong>2000) {
                Toast.makeText(MainActivity.this,"再按一次退出", Toast.LENGTH_SHORT).show();
                elong= System.currentTimeMillis();
            }
            else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}